home *** CD-ROM | disk | FTP | other *** search
- Path: thor.tu.hac.com!collins
- From: collins@thor.tu.hac.com (Ron Collins)
- Newsgroups: comp.lang.c
- Subject: Re: Help, best way to compare doubles
- Date: 18 Jan 1996 17:46:15 GMT
- Organization: Advanced Depot Systems
- Message-ID: <4dm117$6a6@hacgate2.hac.com>
- References: <4da169$mm4@mercury.IntNet.net> <4dfdtl$phv@nntp.crl.com> <DLD5Kp.JME@eskimo.com>
- NNTP-Posting-Host: thor.tu.hac.com
-
- mAg (mag@eskimo.com) wrote:
- : In article <4dfdtl$phv@nntp.crl.com> (16 Jan 1996 05:43:17 GMT),
- : bivey@ecf2.puc.edu says :
- : >
- : >Jeff Tomich (jtomich@IntNet.net) wrote:
- : >: Looking for a way to compare doubles for equality.
- : >
- : >: thanks, Jeff
- : >
- : >The first question to ask is "how equal"? In general, one picks some
- : >error limit, such as e = 1E-6; then uses it in this form:
- : >
- : > if (fabs(n1 - n2) < e) // then they are considered equal
- : > else // they are considered different
- : >
-
- : "How equal" is irrelevant. "Equal to" comparison produces a boolean result
- : True or False.
-
- : '==' is good enough
-
- This is fine if you are comparing for *exact bit pattern* matches. If,
- OTOH, you have done a significant amount of number crunching, then it
- is only prudent to anticipate a certain amount of propagated round-off
- error. In this case, you may wish to consider n1 and n2 "equal" (representing
- the same value), without expecting the same bit pattern stored in the
- variables. For these cases, using an epsilon compare (fabs(n1 - n2) < e)
- is much more meaningfull than a flat "==" comparison.
-
- "How equal" is indeed relevant.
-
-
-
- -- Collins --
-
- -----
- The views expressed here are mine alone.
-
- Ron Collins/Hughes Aircraft Company/M20,P20/Tucson Az 85706
- rcollins@thor.tu.hac.com collins@seagull.rtd.com
- ยก----
-